package com.ag.oa.core.controller;

import com.ag.oa.core.service.IWeeklyService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yxm on 18/10/2021.
 * 工作周报控制器类
 */
@RestController
@RequestMapping({"/weekly", "/test/weekly"})
public class WeeklyController {

    @Autowired
    private IWeeklyService weeklyService;

    private static final Logger logger = LoggerFactory.getLogger(WeeklyController.class);

    /**
     * 分页获取工作周报记录
     */
    @RequestMapping(value = "/report/pageList", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getWeeklyReportPageList(Integer userId, Integer page) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            weeklyService.getWeeklyReportForPageList(userId, page, res);
        }catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 保存新建工作周报
     */
    @RequestMapping(value = "/report/save", produces = "text/json;charset=UTF-8")
    public @ResponseBody String saveNewWorkWeeklyReport(Integer userId, String startDate, String endDate) {

        JSONObject res = new JSONObject();
        try {
            weeklyService.saveNewWeeklyReport(userId, startDate, endDate, res);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            res.put("resCode", 500);
        }
        return res.toJSONString();
    }

    /**
     * 根据周报 id 获取对应的周报工作内容
     * @param id 周报 t_work_report 表 id
     */
    @RequestMapping(value = "/work/contents", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getWeeklyWorkContentList(Integer id) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            weeklyService.getUserWorkContentList(id, res);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 保存工作周报内容
     * @param id 周报工作内容表对应的 id
     * @param weeklyId 周报 id
     * @param taskId 对应任务 id
     * @param contentStr 工作内容
     */
    @RequestMapping(value = "/report/content/save", produces = "text/json;charset=UTF-8")
    public @ResponseBody String saveMyWeeklyWorkContent(Integer id, Integer weeklyId, Integer taskId
            , String contentStr) {

        JSONObject res = new JSONObject();
        int code;

        try {
            code = weeklyService.saveWeeklyWorkContent(id, weeklyId, taskId, contentStr);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 获取工作周报内容
     */
    @RequestMapping(value = "/workContent/detail", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getWeeklyWorkContentDetail(Integer contentId) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            String content = weeklyService.getWeeklyWorkContent(contentId);
            res.put("workContent" , content);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 获取任务进度周报信息
     */
    @RequestMapping(value = "/task/progress/content", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getTaskProgressWorkContent(Integer taskId) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            JSONArray result = weeklyService.getTaskProgressWorkContentList(taskId);
            res.put("progressArr", result);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 删除周报
     */
    @RequestMapping(value = "/delWorkReport")
    public String ajaxDelWeeklyReport(Integer id, Integer contentId) {

        JSONObject res = new JSONObject();
        int code = 200;

        try {
            weeklyService.delWeeklyReport(id, contentId);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage(), e);
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

}
